//
//  SignInController.m
//  MY Project For Sport
//
//  Created by admin on 16/2/11.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "SignInController.h"
#import "NetworkingHandlerOfBlock.h"
#import "ModelOfSignIn.h"
#import "SignInOfCell.h"
#import "SignInDetailCell.h"
#import "SingIn2Controller.h"
#import "SignInWebController.h"
#import <DKNightVersion.h>

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface SignInController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrayForBasket;
@property (nonatomic, retain)NSMutableArray *arrayForFoot;
@end

@implementation SignInController

- (void)dealloc {

    [_tableView release];
    [_arrayForBasket release];
    [_arrayForFoot release];
    [super dealloc];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 隐藏 navigationBar上面的返回按钮
   self.navigationItem.hidesBackButton = YES;
    
    [self handleData];
    [self createTableView];
   
    
}

- (void)handleData {

NSString *strForBasket = @"http://platform.sina.com.cn/sports_client/team_hot?app_key=2586208540&_version__=3.2.1.01&__os__=android&type=2&num=4&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
    
    NSString *strForFoot = @"http://platform.sina.com.cn/sports_client/team_hot?app_key=2586208540&_version__=3.2.1.01&__os__=android&type=1&num=4&client_deviceid=e1626cc2f63146d9302c32f415dfedf0";
    
    [NetworkingHandler networkingWithUrl:strForBasket completeHandle:^(NSURLResponse *response, NSData *data, id result) {
        
        self.arrayForBasket = [NSMutableArray array];
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dict in array) {
            ModelOfSignIn *model = [[ModelOfSignIn alloc] init];
            
            [model setValuesForKeysWithDictionary:dict];
            
            [self.arrayForBasket addObject:model];
            
            [model release];
            
            
        }
        
        
        [self.tableView reloadData];
        
        
    }];
    
    
    [NetworkingHandler networkingWithUrl:strForFoot completeHandle:^(NSURLResponse *response, NSData *data, id result) {
       
        self.arrayForFoot = [NSMutableArray array];
        
        
        NSDictionary *dic = [result objectForKey:@"result"];
        NSArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dict in array) {
            ModelOfSignIn *model = [[ModelOfSignIn alloc] init];
            
            [model setValuesForKeysWithDictionary:dict];
            
            [self.arrayForFoot addObject:model];
            
            [model release];

        }
        
        [self.tableView reloadData];
        
    }];
    

}


- (void)createTableView {

    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor grayColor]);
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[SignInOfCell class] forCellReuseIdentifier:@"poolForSingIn"];
    [self.tableView registerClass:[SignInDetailCell class] forCellReuseIdentifier:@"poolForSignInDetail"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;

}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (section == 0) {
        
        NSString *str = @"篮球俱乐部排行";
                
        return str;
    } else {
    
    
    NSString *str = @"足球俱乐部排行";
        
        return str;
    
    }


}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 40 * HEIGHTT;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section == 0) {
        return self.arrayForBasket.count + 1;
    }else {
    
    
        return self.arrayForFoot.count  + 1;
    
    
    }
    

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    SignInOfCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForSingIn"];
    SignInDetailCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"poolForSignInDetail"];
    
    
    if (indexPath.section == 0) {
        
        if (indexPath.row != 4) {
            ModelOfSignIn *model = [self.arrayForBasket objectAtIndex:indexPath.row];
            
            [cell passModelForSignIN:model];
            
            return cell;
            
        }else {
        
            return cell2;
        
        }
        
    } else {
    
        if (indexPath.row != 4) {
            ModelOfSignIn *model = [self.arrayForFoot objectAtIndex:indexPath.row];
            [cell passModelForSignIN:model];
            
            
            return cell;
            
        }else {
        
            
            
            return cell2;
        
        }
    
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return self.view.frame.size.height / 8;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    if (indexPath.section == 0 && indexPath.row == 4) {
        
        SingIn2Controller *signIn = [[SingIn2Controller alloc] init];
        
        signIn.ID = @"1";
        
        [self.navigationController pushViewController:signIn animated:YES];
        
        
        [signIn release];
        
    }else if (indexPath.section == 1 && indexPath.row == 4){
    
        SingIn2Controller *signIn = [[SingIn2Controller alloc] init];
        
        
        signIn.ID = @"2";

        
        [self.navigationController pushViewController:signIn animated:YES];
        
        
        [signIn release];
    
    
    }else {
    
    
        SignInWebController *web = [[SignInWebController alloc] init];
        
        [self.navigationController pushViewController:web animated:YES];
        
        [web release];
    
    
    }
 
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
